import { ref } from "@vue/composition-api";
import { Notification, Message, MessageBox } from "fawkes-lib";
export function useDeleteConfirmation() {
  const isDeleting = ref(false);

  const deleteData = (selectData, doDelete) => {
    // isDeleting状态用于表示是否正在执行删除操作
    if (isDeleting.value) return;

    if (!selectData.length) {
      Notification({
        title: "警告",
        message: "请选择数据，再删除！",
        type: "warning",
      });
      return;
    }

    MessageBox.confirm("此操作将删除数据，是否继续?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      deleteMode: true,
      type: "warning",
    })
      .then(() => {
        isDeleting.value = true; // 设置正在删除的状态
        if (doDelete && typeof doDelete === "function") {
          // 调用传入的doDelete函数进行删除
          doDelete(selectData).finally(() => {
            isDeleting.value = false; // 清除正在删除的状态
          });
        }
      })
      .catch(() => {
        Message({
          type: "info",
          message: "取消删除",
        });
      });
  };

  // 返回hook的API：deleteData函数和isDeleting状态
  return { deleteData, isDeleting };
}
